/*
 * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */
#include "main.h"
using namespace std;

lv_ui guider_ui;

// GPIO assignment
#define LED_STRIP_BLINK_GPIO GPIO_NUM_13
// Numbers of the LED in the strip
#define LED_STRIP_LED_NUMBERS 1
// 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
#define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000)

static char *TAG_MAIN = "Main";

led_strip_handle_t led_strip;

led_strip_handle_t configure_led(void)
{
    // LED strip general initialization, according to your led board design
    led_strip_config_t strip_config = {
        .strip_gpio_num = LED_STRIP_BLINK_GPIO,   // The GPIO that connected to the LED strip's data line
        .max_leds = LED_STRIP_LED_NUMBERS,        // The number of LEDs in the strip,
        .led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip
        .led_model = LED_MODEL_WS2812,            // LED strip model
    };
    strip_config.flags.invert_out = false;

    // LED strip backend configuration: RMT
    led_strip_rmt_config_t rmt_config = {
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
        .rmt_channel = 0,
#else
        .clk_src = RMT_CLK_SRC_DEFAULT,        // different clock source can lead to different power consumption
        .resolution_hz = LED_STRIP_RMT_RES_HZ, // RMT counter clock frequency
#endif
    };
    rmt_config.flags.with_dma = true; // Use RMT with DMA

    // LED Strip object handle
    led_strip_handle_t led_strip;
    ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
    ESP_LOGI(TAG_MAIN, "Created LED strip object with RMT backend");
    return led_strip;
}

void bsp_dart_charger_init(void)
{
    // GPIO EN Pin Disable
    gpio_set_direction(WIRELESS_CHARGE_EN_PIN, GPIO_MODE_OUTPUT);
    gpio_set_level(WIRELESS_CHARGE_EN_PIN, 0);

    // LCD Touch Reset
    gpio_set_direction(GT911_INT_PIN, GPIO_MODE_OUTPUT);
    gpio_set_direction(GT911_RST_PIN, GPIO_MODE_OUTPUT);

    gpio_set_level(GT911_INT_PIN, 0);
    gpio_set_level(GT911_RST_PIN, 0);

    vTaskDelay(10);
    gpio_set_level(GT911_RST_PIN, 1);

    vTaskDelay(6);
    gpio_set_direction(GT911_INT_PIN, GPIO_MODE_INPUT);

    led_strip = configure_led();

    bsp_i2c_init();
}

extern "C" void app_main(void)
{
    // EventLoop
    esp_event_loop_args_t loop_args = {
        .queue_size = 10,
        .task_name = "dartEventsLoop",
        .task_priority = 5,
        .task_stack_size = 8192,
    };
    ESP_ERROR_CHECK(esp_event_loop_create(&loop_args, &dart_events_loop_handle));

    bsp_dart_charger_init();
    lv_disp_t *disp = bsp_display_start();

#if CONFIG_BSP_DISPLAY_LVGL_AVOID_TEAR
    ESP_LOGI(TAG_MAIN, "Avoid lcd tearing effect");
#if CONFIG_BSP_DISPLAY_LVGL_FULL_REFRESH
    ESP_LOGI(TAG_MAIN, "LVGL full-refresh");
#elif CONFIG_BSP_DISPLAY_LVGL_DIRECT_MODE
    ESP_LOGI(TAG_MAIN, "LVGL direct-mode");
#endif
#endif

    ESP_LOGI(TAG_MAIN, "Showing Init Screen");
    /**
     * To avoid errors caused by multiple tasks simultaneously accessing LVGL,
     * should acquire a lock before operating on LVGL.
     */
    bsp_display_lock(0);

    // UI Setup
    setup_ui(&guider_ui);
    /* Release the lock */
    bsp_display_unlock();
    vTaskDelay(1000);
    dartChargerAppSetup(&guider_ui);

    while (1)
    {
        // LED 彩色呼吸灯，HSV 颜色空间滚动
        for (int i = 0; i < 360; i++)
        {
            uint8_t r, g, b;
            led_strip_set_pixel_hsv(led_strip, 0, i, 255, 50);
            led_strip_refresh(led_strip);
            vTaskDelay(pdMS_TO_TICKS(10));
        }
    }
}